<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch"
             label-width="68px">
      <el-form-item label="员工姓名" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入员工姓名"
          clearable
          size="small"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="staffPhone">
        <el-input
          v-model="queryParams.staffPhone"
          placeholder="请输入手机号码"
          clearable
          size="small"
          maxlength="20"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工类型" prop="staffType">
        <el-select v-model="queryParams.staffType" placeholder="请选择员工类型" clearable
                   size="small">
          <el-option
            v-for="dict in staffOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="认证状态" prop="authenticationStatus">
        <el-select v-model="queryParams.authenticationStatus" placeholder="请选择认证状态" clearable
                   size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:staff:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:staff:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:staff:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="staffList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" width="80" key="staffId" prop="staffId"
                       v-if="columns[0].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="企业名称" align="center" key="company.compName" prop="company.compName"
                       v-if="columns[1].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="员工姓名" align="center" key="staffName" prop="staffName" v-if="columns[2].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="员工邮箱" align="center" key="staffEmail" prop="staffEmail" v-if="columns[3].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="手机号码" align="center" key="staffPhone" prop="staffPhone" v-if="columns[4].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="员工类型" align="center" key="staffType" prop="staffType" :formatter="typeFormat"
                       v-if="columns[5].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="认证状态" align="center" key="authenticationStatus" prop="authenticationStatus"
                       :formatter="statusFormat" v-if="columns[6].visible" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:staff:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:staff:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="compId">
          <el-select v-model="form.compId" placeholder="请选择企业" style="width: 380px;">
            <el-option
              v-for="item in compOptions"
              :key="item.comp"
              :label="item.compName"
              :value="item.compId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="员工姓名" prop="staffName">
          <el-input v-model="form.staffName" placeholder="请输入员工姓名" maxlength="30"/>
        </el-form-item>
        <el-form-item label="员工邮箱" prop="staffEmail">
          <el-input v-model="form.staffEmail" placeholder="请输入员工邮箱" maxlength="50"/>
        </el-form-item>
        <el-form-item label="手机号码" prop="staffPhone">
          <el-input v-model="form.staffPhone" placeholder="请输入手机号码" maxlength="20"/>
        </el-form-item>
        <el-form-item label="员工类型" prop="staffType">
          <el-select v-model="form.staffType" placeholder="请选择员工类型" style="width: 380px;">
            <el-option
              v-for="dict in staffOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listStaff,
  getStaff,
  delStaff,
  addStaff,
  updateStaff,
  exportStaff
} from "@/api/server/companies/staff";

export default {
  name: "Staff",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工信息表格数据
      staffList: [],
      //企业选项
      compOptions: [],
      //状态字典
      statusOptions: [],
      //员工类型字典
      staffOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        compId: null,
        staffName: null,
        staffEmail: null,
        staffPhone: null,
        staffFingermark: null,
        staffType: null,
        authenticationStatus: null,
      },
      // 表单参数
      form: {},
      //列信息
      columns: [
        {key: 0, label: "序号", visible: true},
        {key: 1, label: "企业编号", visible: true},
        {key: 2, label: "员工姓名", visible: true},
        {key: 3, label: "员工邮箱", visible: true},
        {key: 4, label: "手机号码", visible: true},
        {key: 5, label: "员工类型", visible: true},
        {key: 6, label: "认证状态", visible: true}
      ],
      queryForm: {
        staffPhone: [{pattern: /^[0-9]*$/, message: '请输入数字', trigger: 'blur'}],
        staffName: [{pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
      },
      // 表单校验
      rules: {
        compId: [
          {required: true, message: '企业名称不能为空', trigger: 'blur'}
        ],
        staffName: [
          {required: true, message: '员工姓名不能为空', trigger: 'blur'}
        ],
        staffEmail: [
          {
            required: true,
            pattern: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/,
            message: '请输入正确的邮箱',
            trigger: 'blur'
          }
        ],
        staffPhone: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ],
        staffType: [
          {required: true, message: '员工类型不能为空', trigger: 'change'}
        ],
      }
    }
      ;
  },
  created() {
    this.getList();
    this.getDicts('t_staff_status').then(response => {
      return this.statusOptions = response.data
    })
    this.getDicts('t_staff_type').then(response => {
      return this.staffOptions = response.data
    })
  },
  methods: {
    /** 查询员工信息列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        let arr = response.rows
        for (let i in arr) {
          this.staffList[i].staffPhone = arr[i].staffPhone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
        }
        this.total = response.total;
        this.loading = false;
      })
      ;
    },
    //状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.authenticationStatus)
    },
    //员工类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.staffOptions, row.staffType)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        compId: "",
        compOptions: "",
        staffName: "",
        staffEmail: "",
        staffPhone: "",
        staffType: "",
        remark: null
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.$refs['queryForm'].validate(valid => {
        if (valid) {
          this.getList()
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.staffId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getStaff().then(response => {
        this.compOptions = response.comps;
        this.open = true;
        this.title = "添加员工信息";
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const staffId = row.staffId || this.ids
      getStaff(staffId).then(response => {
        this.form = response.data;
        this.compOptions = response.comps
        this.open = true;
        this.title = "修改员工信息";
      })
      ;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.staffId != undefined) {
              updateStaff(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              ;
            } else {
              console.log(this.form)
              addStaff(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              ;
            }
          }
        }
      )
      ;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const staffIds = row.staffId || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delStaff(staffIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有员工信息数据项?', "提示", {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: "info"
      }).then(function () {
        return exportStaff(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
